/*
 * Copyright (c) $today.year-Now http://www.zxpnet.com All rights reserved.
 */

package com.zxp.springboot.controller;

import cn.hutool.core.util.RandomUtil;
import com.zxp.springboot.pojo.User;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 见WebMvcAutoConfiguration类源码： 会实例化一个HiddenHttpMethodFilter
 * @desc:
 * @author: shollin
 * @date: 2021/6/20/020 6:32
 */
@RestController
public class RestfulDemoController {

    /**
     * http://localhost:19090/user?format=xml  自定义协商
     * @return
     */
    @GetMapping("/user")
    public User get() {
        User user = randomUser();
        return user;
    }

    @PostMapping("/user")
    public String post() {

        return "PostMapping...";
    }
    @PutMapping("/user")
    public String put() {

        return "PutMapping...";
    }

    @DeleteMapping("/user")
    public String del() {

        return "DeleteMapping...";
    }


    /**
     * 生成随机用户
     * @return
     */
    private User randomUser(){
        User helloUser = new User();
        helloUser.setUsername(RandomUtil.randomString(8));
        helloUser.setName(RandomUtil.randomString(6));
        helloUser.setAge(RandomUtil.randomInt(20,50));
        helloUser.setSalary(RandomUtil.randomBigDecimal(new BigDecimal("2000"),new BigDecimal("50000")) );
        helloUser.setCreateTime(RandomUtil.randomDay(-365*5,365*5));
        helloUser.setBirthday( LocalDate.now().minusDays(RandomUtil.randomLong(20*365,60*365)));
        helloUser.setUpdateTime(LocalDateTime.now());
        helloUser.setSex("M");
        helloUser.setSuperAdminFlag("N");
        helloUser.setStatusFlag(1);
        helloUser.setDelFlag("N");
        return helloUser;
    }
}
